<?php
namespace App\Models\Chain\PointCash;




use App\Constants\CreditLogConstant;
use App\Models\Factory\InviteFactory;
use App\Models\Chain\AbstractHandler;
use App\Models\Factory\UserCreditFactory;
use App\Models\Orm\AwUserIntegralLog;
use App\Models\Orm\User;

/**
 * 插入用户积分日志表
 * Class CreateCreditLogAction
 * @package App\Models\Chain\Identity
 */

class CreateUserCreditLogAction extends AbstractHandler
{
    private $params = array();

    protected $error = array('error' => '插入积分日志失败', 'code' => 7011);


    public function __construct($params)
    {
        $this->params = $params;
    }

    public function handleRequest()
    {
        if ($this->createUserCreditLog($this->params) == true) {
            $this->setSuccessor(new CreateUserAccountLogAction($this->params));
            return $this->getSuccessor()->handleRequest();
        } else {
            return $this->error;
        }
    }


    public function createUserCreditLog($params)
    {

        $user = User::where('id',$params['userId'])->first();
        $action = AwUserIntegralLog::ACTION_EXCHANGE;
        $type = AwUserIntegralLog::TYPE_CUT;
        $userIntegral = $user->getIntegral()->integral;
        $status =  AwUserIntegralLog::STATUS_ACCESS;

        $result = UserCreditFactory::createCreditLogInvite($params['order_id'],$params['userId'],$params['credit'],$action,$type,$status,$userIntegral);
        return $result;
    }


}